package com.limedia.explorer.service;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//import com.heeere.android.dnssdtuto.DeviceDiscoverCallback;
//import com.heeere.android.dnssdtuto.DnssdDiscovery;
import com.limedia.explorer.constant.Constant;
import com.limedia.explorer.transmission.StreamUploader;

import android.R.integer;
import android.R.menu;
import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Message;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import limedia.sdk.api.realtime.TSRTSPNative;

public class DeviceDiscoverService extends Service {
	
	private static final String TAG = DeviceDiscoverService.class.getSimpleName();
//	private DeviceDiscoverCallback searchListener = null;
	private LocalBroadcastManager mBroadcastManager = null;
//	private DnssdDiscovery mDnss = null;
	
	
//	private StreamUploader streamUploader = null;
//	private UploadHandlerThread mHandlerThread = null;
	private Handler mHandler = null;
	
	@Override
	public void onCreate() {
		super.onCreate();
		// Make this service fore-ground.
//		Notification notificatiion = new Notification();
//        startForeground(1, notificatiion);
		Log.d(TAG, "onCreate()");
		this.init();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.d(TAG, "onStartCommand()");
		
		ExecutorService executor = Executors.newSingleThreadExecutor();
		executor.submit(new Runnable() {
			
			@Override
			public void run() {
				Log.d(TAG, "mDnss.setUp()");
//				mDnss.setUp(getApplicationContext(), searchListener);
			}
		});
		return super.onStartCommand(intent, flags, startId);
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void onDestroy() {
		Log.d(TAG, "onDestroy()");
//		if(null != this.mDnss) {
//			Log.d(TAG, "mDnss.stop()");
//			this.mDnss.stop();
//		}
		
//		if(null != this.mHandlerThread) {
//			this.mHandlerThread.quitThread();
//			this.mHandlerThread = null;
//			this.mHandler = null;
//		}
//		
//		if(null != this.streamUploader) {
//			this.streamUploader.stopRun();
//			this.streamUploader = null;
//		}
		
		super.onDestroy();
	}
	
	private void init() {
		
		this.mBroadcastManager = LocalBroadcastManager.getInstance(getApplicationContext());
		
//		this.mDnss = new DnssdDiscovery();		
//		this.searchListener = new DeviceDiscoverCallback() {
//
//			@Override
//			public void onServiceResolved(String ip) {
//				ip = ip.replace("/", "");
//				Log.d(TAG, "onServiceResolved(): ip is " + ip);
//				Intent intent = new Intent(Constant.ACTION_SERVICE_RESOLVED);
//				intent.putExtra("ip", ip);
//				mBroadcastManager.sendBroadcast(intent);
//			}
//
//			@Override
//			public void onServiceRemoved(String name) {
//				Log.d(TAG, "onServiceRemoved(): name is " + name);
//				mBroadcastManager.sendBroadcast(new Intent(Constant.ACTION_SERVICE_REMOVED));
//			}
//
//			@Override
//			public void onServiceAdded() {
//				Log.d(TAG, "onServiceAdded()");
//				mBroadcastManager.sendBroadcast(new Intent(Constant.ACTION_SERVICE_ADDED));
//			}
//		};
		
//		this.streamUploader = new StreamUploader();
//		this.streamUploader.start();
//		this.mHandlerThread = new UploadHandlerThread(UploadHandlerThread.class.getSimpleName());
//		this.mHandlerThread.start();
//		this.mHandler = new Handler(this.mHandlerThread.getLooper(), this.mHandlerThread);
//		TSRTSPNative.getInstance().setmHandler(this.mHandler);
	}
	
	private void onUploadDataArrived() {
//		if(null != this.streamUploader) {
//			synchronized (this.streamUploader) {
//				this.streamUploader.notify();
//			}
//		}
	}
	
	public class UploadHandlerThread extends HandlerThread implements Callback {

		public static final int MSG_DATA_ARRIVED = 1;
		
		public UploadHandlerThread(String name) {
			super(name);
		}
		
		@Override
		public boolean handleMessage(Message msg) {
			
			switch (msg.what) {
			case MSG_DATA_ARRIVED:
				onUploadDataArrived();
				break;

			default:
				break;
			}
			
			return true;
		}
		
		public void quitThread() {
			this.getLooper().quit();
			this.interrupt();
			try {
				this.join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
 }
